import {createSlice} from "@reduxjs/toolkit";
import {getToken, setToken as _setToken, removeToken} from '@/util/auth'

const userStore = createSlice({
    name: 'user',
    initialState: {
        userInfo: {},
        token: getToken()
    },
    reducers: {
        setUserInfo(state, { payload }) {
            state.userInfo = payload
        },
        setToken(state, { payload }) {
            state.token = payload
            _setToken(payload)
        },
        clearUserInfo(state, { payload }) {
            state.token = ''
            state.userInfo = {}
            removeToken()
        }
    }
})
export const {setUserInfo, setToken, clearUserInfo} = userStore.actions
export default userStore.reducer
